package org.loofer.ext.utils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class CryptoUtils {

    public static class AES {
        public static final String apikey = "BaweTw.lc!)61K{9^5";

        public static String encrypt(String key, String clearText) throws Exception {
            return CryptoUtils.bytesToHexString(encrypt(getRawKey(key), clearText.getBytes("UTF-8")));
        }

        public static byte[] encrypt(byte[] key, byte[] clearText) throws Exception {
            SecretKeySpec skeySpec = new SecretKeySpec(key, 0, Math.min(key.length, Cipher.getMaxAllowedKeyLength("AES") / 8), "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(1, skeySpec, new IvParameterSpec(key, 0, cipher.getBlockSize()));
            return cipher.doFinal(clearText);
        }

        public static String decrypt(String key, String encryptedText) throws Exception {
            return new String(decrypt(getRawKey(key), CryptoUtils.hexStringToBytes(encryptedText)), "UTF-8");
        }

        public static byte[] decrypt(byte[] key, byte[] encryptedText) throws Exception {
            SecretKeySpec skeySpec = new SecretKeySpec(key, 0, Math.min(key.length, Cipher.getMaxAllowedKeyLength("AES") / 8), "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(2, skeySpec, new IvParameterSpec(key, 0, cipher.getBlockSize()));
            return cipher.doFinal(encryptedText);
        }

        private static byte[] getRawKey(String key) throws Exception {
            return MessageDigest.getInstance("MD5").digest(key.getBytes("UTF-8"));
        }
    }

    public static class Base64 {
        public static String toBase64(byte[] input) {
            return android.util.Base64.encodeToString(input, 0);
        }

        public static String toBase64(byte[] input, int flags) {
            return android.util.Base64.encodeToString(input, flags);
        }

        public static byte[] fromBase64(byte[] input) {
            return android.util.Base64.decode(input, 0);
        }

        public static byte[] fromBase64(byte[] input, int flags) {
            return android.util.Base64.decode(input, flags);
        }

        public static byte[] fromBase64String(String input) {
            return android.util.Base64.decode(input, 0);
        }

        public static byte[] fromBase64String(String input, int flags) {
            return android.util.Base64.decode(input, flags);
        }
    }

    public static class MD5 {
        public static byte[] toMd5(String input) {
            try {
                return toMd5(input, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                return new byte[0];
            }
        }

        public static byte[] toMd5(String input, String charsetName) throws UnsupportedEncodingException {
            return toMd5(input.getBytes(charsetName));
        }

        public static byte[] toMd5(byte[] data) {
            try {
                MessageDigest digest = MessageDigest.getInstance("MD5");
                digest.update(data);
                return digest.digest();
            } catch (NoSuchAlgorithmException e) {
                return new byte[0];
            }
        }

        public static String toMD5(String s) {
            try {
                MessageDigest md = MessageDigest.getInstance("MD5");
                md.update(s.getBytes());
                byte[] bs = md.digest();
                StringBuffer sb = new StringBuffer();
                for (byte b : bs) {
                    int v = b & 255;
                    if (v < 16) {
                        sb.append(0);
                    }
                    sb.append(Integer.toHexString(v));
                }
                return sb.toString();
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
                return "";
            }
        }

        public static String toMd532(String sourceStr) {
            String result = "";
            try {
                MessageDigest md = MessageDigest.getInstance("MD5");
                md.update(sourceStr.getBytes());
                byte[] b = md.digest();
                StringBuffer buf = new StringBuffer("");
                for (int i : b) {
                    int i2;
                    if (i2 < (byte) 0) {
                        i2 += 256;
                    }
                    if (i2 < 16) {
                        buf.append("0");
                    }
                    buf.append(Integer.toHexString(i2));
                }
                result = buf.toString().toLowerCase();
            } catch (NoSuchAlgorithmException e) {
                System.out.println(e);
            }
            return result;
        }
    }

    public static String bytesToHexString(byte[] input) {
        StringBuffer sb = new StringBuffer(input.length);
        for (byte anInput : input) {
            String sTemp = Integer.toHexString(anInput & 255);
            if (sTemp.length() < 2) {
                sb.append(0);
            }
            sb.append(sTemp.toUpperCase());
        }
        return sb.toString();
    }

    public static byte[] hexStringToBytes(String hex) {
        int len = hex.length() / 2;
        byte[] result = new byte[len];
        char[] achar = hex.toCharArray();
        for (int i = 0; i < len; i++) {
            int pos = i * 2;
            result[i] = (byte) ((hexToByte(achar[pos]) << 4) | hexToByte(achar[pos + 1]));
        }
        return result;
    }

    private static byte hexToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }
}
